#include "ControladorMouseCabina.h"


ControladorMouseCabina::ControladorMouseCabina(Escena* escena):ControladorMouse(escena){
	//TODO
}


ControladorMouseCabina::~ControladorMouseCabina(void)
{
}

void ControladorMouseCabina::onPassiveMotion(int x, int y) {
	//TODO
}

void ControladorMouseCabina::onMotion(int x, int y) {
	//TODO
}

void ControladorMouseCabina::moverCamara(int despX, int despY) {
	GLfloat xant = this->escena->getCamara()->getCentre(0);
	GLfloat yant = this->escena->getCamara()->getCentre(1);
	GLfloat zant = this->escena->getCamara()->getCentre(2);
	GLfloat angDesp = (despX/5.55)* PI / 180;
	GLfloat angDespZ = (despY/5.55)* PI / 180;
	GLfloat angAnt = 0;
	if(xant == 0){
		if( yant > 0 )
			angAnt = PI / 2.0;
		else
			angAnt = -PI / 2.0 ;
	}else {
		angAnt = atan(yant/xant) ;
	}
	GLfloat largoxy = sqrt(xant*xant + yant*yant);

	GLfloat angAntz = 0;
	if(largoxy == 0) {
		if( zant > 0 )
			angAntz = PI / 2.0;
		else
			angAntz = -PI / 2.0 ;
	} else {
		angAntz = atan(zant/largoxy);
	}
	GLfloat z = sin ( angAntz + angDespZ) * DIST_XYZ;
	if( (angAntz + angDespZ) > PI/2 && (angAntz + angDespZ) < (2*PI-PI/2))
		this->escena->getCamara()->setUp(0.0,0.0,-1.0);
	else
		this->escena->getCamara()->setUp(0.0,0.0,1.0);
	largoxy = cos (angAntz + angDespZ) * DIST_XYZ;

	GLfloat x = cos(angAnt + angDesp) * largoxy;
	GLfloat y = sin(angAnt + angDesp) * largoxy;

	this->escena->getCamara()->setCentre(x,y,z);
	glutPostRedisplay();
}